package com.cysyz.json.utils;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateJsonValueProcessor implements JsonValueProcessor {

    private static Map<String, DateFormat> dateFormatMap = new HashMap<String, DateFormat>();

    static {
        dateFormatMap.put("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd"));
        dateFormatMap.put("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd"));
        dateFormatMap.put("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd"));

    }

    private DateFormat dateFormat = dateFormatMap.get("yyyy-MM-dd");

    public DateJsonValueProcessor(String datePattern){
        try{  
            dateFormat  = new SimpleDateFormat(datePattern);
              
        } catch (Exception e ){
            dateFormat = dateFormatMap.get("yyyy-MM-dd");
        }
    }

    public Object processArrayValue(Object value, JsonConfig jsonConfig) {
        return process(value);  
    }  
  
    public Object processObjectValue(String key, Object value,  
            JsonConfig jsonConfig) {
        return process(value);  
    }  
    private Object process(Object value){  
        return dateFormat.format((Date)value);
          
    }  
} 